from rest_framework.views import APIView
from rest_framework.response import Response
from api.serializer.account import MessageSerializer
import random
from django_redis import get_redis_connection


class MessageView(APIView):
    def get(self, request, *args, **kwargs):
        ser = MessageSerializer(data=request.query_params)
        if not ser.is_valid():
            print(ser.errors)
            return Response({'status': False, 'message': ser.errors['phone'][0]})
        phone = request.query_params.get('phone')

        random_code = str(random.randint(1000, 9999))

        # send_message(phone, random_code)
        print(random_code)
        conn = get_redis_connection()
        conn.set(phone, random_code, ex=60)

        return Response({"status": True, "code": random_code})
